package app

import (
	"context"
	"kstar-gf-api/app/admin/crontab"
	"kstar-gf-api/app/middleware"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcache"
	"github.com/gogf/gf/v2/os/gsession"

	_ "kstar-gf-api/app/admin/router"
	_ "kstar-gf-api/app/mini/router"
	_ "kstar-gf-api/app/validator"

	_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
	_ "github.com/gogf/gf/contrib/nosql/redis/v2"

	_ "kstar-gf-api/internal/logic"
)

func Run() {
	// 启动定时器
	crontab.CrontabService.Start()

	s := g.Server()

	// 设置session存储模式
	s.SetSessionStorage(gsession.NewStorageRedisHashTable(g.Redis()))
	g.DB().GetCache().SetAdapter(gcache.NewAdapterRedis(g.Redis()))

	//注册全局中间件
	s.Use(middleware.MiddlewareGlobalHandler)

	// 读取平滑重启的配置，平滑重启web管理的url
	adminUrlCfg, _ := g.Cfg().Get(context.TODO(), "server.gracefulAdminUrl")
	adminUrl := adminUrlCfg.String()
	if len(adminUrl) > 0 {
		s.EnableAdmin(adminUrl)
	}

	s.Run()
}
